<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>逐字显示动画</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #f0f2f5;
            margin: 0;
        }
        .container {
            max-width: 800px;
            padding: 2rem;
        }
        h1 {
            font-size: 3rem;
            line-height: 1.3;
            margin-bottom: 1.5rem;
        }
        .char {
            display: inline-block;
            opacity: 0;
        }
        .btn {
            padding: 0.8rem 1.5rem;
            background: #4a6bdf;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
            margin-top: 2rem;
        }
    </style>
</head>
<body>
<div class="container">
    <h1 id="text">逐字显示的动画效果让文本更具吸引力</h1>
    <button class="btn" id="replay">重新播放</button>
</div>

<!-- 无需手动注册，可直接使用 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/SplitText.min.js"></script>
<script>
    // 注册插件
    // gsap.registerPlugin(SplitText);

    let split;
    let tl;

    function initAnimation() {
        // 如果已有实例，先恢复
        if (split) split.revert();
        if (tl) tl.kill();

        // 分割文本
        split = new SplitText("#text", {
            type: "chars",
            charsClass: "char"
        });

        // 设置初始状态
        gsap.set(split.chars, {
            opacity: 0,
            y: 20
        });

        // 创建时间轴动画
        tl = gsap.timeline()
            .to(split.chars, {
                duration: 0.6,
                opacity: 1,
                y: 0,
                stagger: 0.05,
                ease: "back.out"
            });
    }

    // 初始化动画
    document.addEventListener('DOMContentLoaded', initAnimation);

    // 重新播放
    document.getElementById("replay").addEventListener("click", initAnimation);
</script>
</body>
</html>